package com.crawler.util.contex;

import com.crawler.util.StrKit;
import com.crawler.util.html.SourceLink;
import com.crawler.util.http.BaseHttpException;
import com.crawler.util.http.HttpQuery;
import com.crawler.util.template.URLTemplate;
import com.crawler.util.template.URLTemplateBuilder;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.*;

/**
 * 正文配置提取器
 *
 * @author guor
 */
@Slf4j
public class ContexAutoBuilder {
    public Map<String, Set<SourceLink>> build(String sourceUrl) {
        try {
            String html = HttpQuery.getInstance().get(sourceUrl).asString();
            Element body = Jsoup.parse(html, sourceUrl).body();
            if (body == null) {
                return null;
            }
            Elements elements = body.select("a");
            List<SourceLink> links = new ArrayList<>();
            for (Element e : elements) {
                String href = e.absUrl("href");
                String text = e.text();
                if (StrKit.isEmpty(href) || StrKit.isEmpty(text) || text.length() <= 4) {
                    log.debug("链接href：{}，text：{}过短，跳过生成模版串。", href, text);
                    continue;
                }
                links.add(new SourceLink(href, text));
            }
            return build(links);
        } catch (BaseHttpException e) {
            log.error("获取{}页面源码失败。", sourceUrl);
            return null;
        }
    }

    public Map<String, Set<SourceLink>> build(List<SourceLink> links) {
        Map<String, Set<SourceLink>> templates = new HashMap<>(links.size());
        for (SourceLink link : links) {
            URLTemplate template = URLTemplateBuilder.build(link.getUrl());
            if (template == null) {
                continue;
            }
            String t = template.toString();
            log.debug("对URL：{}生成模版：{}", link.getUrl(), t);
            if (templates.containsKey(t)) {
                templates.get(t).add(link);
            } else {
                HashSet<SourceLink> ls = new HashSet<>();
                ls.add(link);
                templates.put(t, ls);
            }
        }
        Set<String> s = new HashSet<>(templates.keySet());
        for (String t : s) {
            if (templates.get(t).size() <= 3) {
                log.debug("模版：{}对应链接个数太少，疑似无效链接，删除。", t);
                templates.remove(t);
            }
        }
        return templates;
    }
}
